package com.anotherrobbo.armedia.server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.anotherrobbo.armedia.engine.PlayerEngine;
import com.anotherrobbo.armedia.media.Media;

public abstract class AbstractReader extends Thread {

	protected final Logger log = LoggerFactory.getLogger(getClass()); 
	protected final PlayerEngine engine;
	
	protected static final String CMD_EXIT = "exit";
	protected static final String CMD_PLAY = "play";
	protected static final String CMD_NEXT = "next";
	protected static final String CMD_PREV = "prev";
	protected static final String CMD_PRINT = "print";

	public AbstractReader(String threadName, PlayerEngine engine) {
		super(threadName);
		this.engine = engine;
	}
	
	@Override
	public void run() {
		read();
	}
	
	protected abstract void read();
	
	protected boolean actionInput(String input) {
		if (matches(CMD_EXIT, input)) {
			log.info("Exiting.");
			return false;
		} else if (matches(CMD_PLAY, input)) {
			engine.play();
		}  else if (matches(CMD_NEXT, input)) {
			engine.next();
		}  else if (matches(CMD_PREV, input)) {
			engine.prev();
		}  else if (matches(CMD_PRINT, input)) {
			Media media = engine.getCurrentMedia();
			log.info(media.getFileName());
		} else {
			log.info("Unknown command: " + input);
		}
		return true;
	}
	
	private boolean matches(String cmd, String line) {
		return cmd.equalsIgnoreCase(line);
	}

}
